package com.sinoglobal.ningxia.activity.vip;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.activity.base.BaseActivity;
import com.sinoglobal.ningxia.beans.RootVo;
import com.sinoglobal.ningxia.downdata.imp.RemoteImpl;
import com.sinoglobal.ningxia.http.Code;
import com.sinoglobal.ningxia.utils.TextUtil;
import com.sinoglobal.ningxia.utils.ValidUtil;
/**
 * 
* @title: FindPasswordActivity.java 
* @package com.sinoglobal.ningxia.activity.vip 
* @description: 找回密码页面 
* @author lms
* @date 2014-6-18 上午11:01:34
 */
public class PasswordFind extends BaseActivity implements
		OnClickListener {
	// 下一步
	private TextView next;
	// 输入手机号码
	private EditText phoneEdit;
	private Intent intent;
	// 手机号码字符串
	private String phoneStr;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_vip_find_password);
		openChenJInShi();//开启沉浸式
		init();
		setListener();
	}

	private void setListener() {
		next.setOnClickListener(this);
	}

	private void init() {
		titleView.setText(R.string.input_phoneNum_title);
		next = (TextView) findViewById(R.id.next_tv);
		phoneEdit = (EditText) findViewById(R.id.edit_phone);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.next_tv://下一步
			phoneStr = phoneEdit.getText().toString().trim();
			String validPhone = ValidUtil.validPhone(phoneStr);
			// 当手机号验证成功时发送验证码，验证不成功时，提示信息
			if (!TextUtil.stringIsNull(validPhone)) {
				showShortToastMessage(validPhone);
				return;
			} else {
				sendCode();
			}

			break;
		}
	}

	// 发送验证码
	private void sendCode() {
		new ItktAsyncTaskWithDialog<Void, Void, RootVo>(getResources().getString(R.string.sending), true, false) {

			@Override
			public RootVo before(Void... params) throws Exception {
				return RemoteImpl.getInstance().getFindPwdCode(phoneStr);
			}

			@Override
			public void after(RootVo result) {
				// 数据为空，显示reLoad
				if (null == result) { 
					showReLoading();
					return;
				}
				//code值不正确时，提示信息
				if (Code.CODE_SUCCESS != result.getCode()) {
					showShortToastMessage(result.getMessage());
					return;
				}
				// 获取成功后跳转到输入验证码页面
				intent = new Intent(PasswordFind.this, CodeInput.class);
				intent.putExtra("phoneNum", phoneStr);
				intent.putExtra("activity", "reset_pwd");
				startActivity(intent);
				pushActivity(PasswordFind.this);
			}

			@Override
			public void exception() {
				dismissWaitingDialog();
				showShortToastMessage(getResources().getString(R.string.loadFail));
			}
		}.execute();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		finish();
	}
}
